package com.zpl.goods.controller;

import com.alibaba.fastjson.JSON;
import com.zpl.core.Result;
import com.zpl.goods.entity.Category;
import com.zpl.goods.entity.CategoryUS;
import com.zpl.goods.entity.vo.CategoryVO;
import com.zpl.goods.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangyuxin
 * @date 2020/9/29
 */
@RestController
@RequestMapping("/category")
@Api(value = "CategoryController", tags = {"类别管理"})
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation(value = "1.添加类别", notes = "添加", httpMethod = "POST")
    @ApiImplicitParam(name = "category", value = "类别信息", dataType = "Category",required = true)
    public Result insert(@RequestBody Category category){
        if (categoryService.insert(category)){
            return Result.success("类别添加成功");
        }
        return Result.fail(category.getCategoryName()+"已存在");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "2.删除类别", notes = "通过id删除类别", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "货物id", dataType = "Long")
    public Result delete(@PathVariable("id") Long id){
        if(categoryService.delete(id)){
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PutMapping
    @ApiOperation(value = "3.更改类别", notes = "通过id更改类别", httpMethod = "PUT")
    @ApiImplicitParam(name = "category", value = "类别信息", dataType = "Category",required = true)
    public Result update(@RequestBody CategoryUS categoryUS) {
        System.out.println(JSON.toJSON(categoryUS));
        if (categoryService.update(categoryUS)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @GetMapping("/alllist")
    @ApiOperation(value = "4.获取所有类别", notes = "获取所有类别", httpMethod = "GET")
    public Result getAllCategory(){
        List<CategoryVO> categoryVOList=categoryService.show();
        return Result.success(categoryVOList);
    }
}
